package com.yanqu.road.dao.impl.activity;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.UnionActivityTaskData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionActivityTaskDaoImpl extends TempDataObjectDao<UnionActivityTaskData> {

    public static String getUnionActivityTaskDataSql = "select * from `t_c_activity_union_task` where `activityId`=? ";

    public Map<String, Map<Integer, UnionActivityTaskData>> getUnionActivityTaskDataMap(int activityId) {
        Map<String, Map<Integer, UnionActivityTaskData>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionActivityTaskDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionActivityTaskData taskData = parse(rs);
                mapMap.putIfAbsent(taskData.getUnionUid(), new ConcurrentHashMap<>());

                Map<Integer, UnionActivityTaskData> map = mapMap.get(taskData.getUnionUid());
                map.put(taskData.getConditionId(), taskData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

    public static String updateUnionActivityTaskDataSql = "update `t_c_activity_union_task` set value=? where activityId=? and unionUid=? and conditionId=?";

    @Override
    protected boolean updateData(UnionActivityTaskData unionActivityTaskData) {
        unionActivityTaskData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, unionActivityTaskData.getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityTaskData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityTaskData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityTaskData.getConditionId()));
        if (getDb().executeNoneQuery(updateUnionActivityTaskDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionActivityTaskDataSql = "insert into `t_c_activity_union_task`(activityId,unionUid,conditionId,value) values (?,?,?,?)";

    @Override
    protected boolean addData(UnionActivityTaskData unionActivityTaskData) {
        unionActivityTaskData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityTaskData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionActivityTaskData.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, unionActivityTaskData.getConditionId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionActivityTaskData.getValue()));
        if (getDb().executeNoneQuery(insertUnionActivityTaskDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionActivityTaskData parseData(ResultSet rs) throws SQLException {
        UnionActivityTaskData unionActivityTaskData = new UnionActivityTaskData();
        unionActivityTaskData.setActivityId(rs.getInt("activityId"));
        unionActivityTaskData.setUnionUid(rs.getString("unionUid"));
        unionActivityTaskData.setConditionId(rs.getInt("conditionId"));
        unionActivityTaskData.setValue(rs.getLong("value"));
        return unionActivityTaskData;
    }
}
